import os
import sys
from functools import reduce
"""
a1:  区间值模糊数 格式为([],[])

self.q: q值

a2:     区间值模糊数 格式为([],[])

编写时间:2022-6-29 
编写人:吴卓成
参考文献:运算法则
"""

from Utilities.AutoGetOperator.selectPackage import get_func

Operator_IVQ_ROFS=get_func(r'Operators/OperationOperators/OperatorIVQROF.py','Operator_IVQ_ROFS')

class Revice(Operator_IVQ_ROFS):
    """

    """
    def add(self,a1,a2,q=0,*waste1, **waste2):
        """
        function: 区间值广义正交模糊数相加

        a1  :     参上
        a2  :     参上
        q   :     参数
        p   :     参数

        return:  返回模糊数相加的结果形式为([],[])
        """
        u_1_f, u_1_r, v_1_f, v_1_r = *a1[0], *a1[1]
        u_2_f, u_2_r, v_2_f, v_2_r = *a2[0], *a2[1]
        
        if q <= 0:
            q = self.q
        u_f =( 1-((1-(u_1_f**q))*(1-(u_2_f**q))))**(1/q)
        u_r =( 1-((1-(u_1_r**q))*(1-(u_2_r**q))))**(1/q)
        v_f =(((1-(u_1_f**q)) * (1-(u_2_f**q))) - ((1-(u_1_f**q )-(v_1_f**q)) * (1-(u_2_f**q)-(v_2_f**q))))**(1/q)
        v_r =(((1-(u_1_r**q)) * (1-(u_2_r**q))) - ((1-(u_1_r**q )-(v_1_r**q)) * (1-(u_2_r**q)-(v_2_r**q))))**(1/q)
        return ([u_f, u_r], [v_f, v_r])
        
    def multi(self,a1,a2,q=0,*waste1, **waste2):
        """
         function: 区间值广义正交模糊数相乘

        a1  :     参上
        a2  :     参上
        q   :     参数

        """
        u_1_f, u_1_r, v_1_f, v_1_r = *a1[0], *a1[1]
        u_2_f, u_2_r, v_2_f, v_2_r = *a2[0], *a2[1]
        if q <= 0:
            q = self.q
        v_f = (1 - ((1 - (v_1_f ** q)) * (1 - (v_2_f ** q)))) ** (1 / q)
        v_r = (1 - ((1 - (v_1_r ** q)) * (1 - (v_2_r ** q)))) ** (1 / q)
        u_f = ((1 - (v_1_f ** q)) * (1 - (v_2_f ** q)) - (
                    (1 - (u_1_f ** q) - (v_1_f ** q)) * (1 - (u_2_f ** q) - (v_2_f ** q)))) ** (1 / q)
        u_r = ((1 - (v_1_r ** q)) * (1 - (v_2_r ** q)) - (
                    (1 - (u_1_r ** q) - (v_1_r ** q)) * (1 - (u_2_r ** q) - (v_2_r ** q)))) ** (1 / q)
        return ([u_f, u_r], [v_f, v_r])
    def kmulti(self,a1,t,q=0,*waste1, **waste2):
        """
         function: 区间值广义正交模糊数的数乘

        a1  :     参上
        a2  :     参上
        q   :     参数
        t   :     数乘的数
        return  : 返回模糊数数乘的结果 依旧是一个模糊数
        """
        u_1_f, u_1_r, v_1_f, v_1_r = *a1[0], *a1[1]
        if q <= 0:
            q = self.q
        v_f=(((1-(u_1_f**q))**t) - ((1-(u_1_f**q)-(v_1_f**q))**t))**(1/q)
        v_r=(((1-(u_1_r**q))**t) - ((1-(u_1_r**q)-(v_1_r**q))**t))**(1/q)
        u_f=(1 - ((1-(u_1_f**q))**t))**(1/q)
        u_r=(1 - ((1-(u_1_r**q))**t))**(1/q)
        return ([u_f, u_r], [v_f, v_r])
        
        
    def pow(self,a1,t,q=0,*waste1, **waste2):
        """
         function: 区间值广义正交模糊数次方

        a1  :     参上
        q   :     参数
        p   :     参数
        t   :     次方数
        return  : 返回模糊数的次方
        """
        u_1_f, u_1_r, v_1_f, v_1_r = *a1[0], *a1[1]
        if q <= 0:
            q = self.q
        u_f=(((1-(v_1_f**q))**t) - ((1-(u_1_f**q)-(v_1_f**q))**t))**(1/q)
        u_r=(((1-(v_1_r**q))**t) - ((1-(u_1_r**q)-(v_1_r**q))**t))**(1/q)
        v_f=(1 - ((1-(v_1_f**q))**t))**(1/q)
        v_r=(1 - ((1-(v_1_r**q))**t))**(1/q)
        return ([u_f, u_r], [v_f, v_r])

class ReviceA(Revice):

    def getResult(self):
        '''
        function：基础的算数平均公式
        :return: AA公式计算的结果
        '''
        result=self.data_list[0]
        for i in range(1,len(self.data_list)):
            result=self.add(result,self.data_list[i])
        result=self.kmulti(result,1/len(self.data_list))

        return result

class ReviceGA(Revice):

    def getResult(self):
        '''
        function：基础的算数平均公式
        :return: AA公式计算的结果
        '''
        result = self.data_list[0]
        for i in range(1, len(self.data_list)):
            result = self.multi(result, self.data_list[i])
        result = self.pow(result, 1 / len(self.data_list))

        return result

class ReviceWA(Revice):

    def getResult(self):
        '''
        function：基础的加权算数平均公式
        :return: WAA公式计算的结果
        '''
        result = self.kmulti(self.data_list[0],self.weight_list[0])
        for i in range(1, len(self.data_list)):
            temp=self.kmulti(self.data_list[i],self.weight_list[i])
            result = self.add(result, temp)

        return result

class ReviceWGA(Revice):

    def getResult(self):
        '''
        function：基础的加权几何平均公式
        :return: WAA公式计算的结果
        '''
        result = self.pow(self.data_list[0],self.weight_list[0])
        for i in range(1, len(self.data_list)):
            temp=self.pow(self.data_list[i],self.weight_list[i])
            result = self.multi(result, temp)

        return result

class ReviceOWA(Revice):

    def getResult(self):
        '''
        function：基础的有序加权算数平均公式
        :return: OWAA公式计算的结果
        '''
        # 将 数据集备份，将类中数据集属性 替换为 排序后的数据集
        data_list = self.data_list
        self.data_list = self.sortdata()

        result = self.kmulti(self.data_list[0],self.weight_list[0])
        for i in range(1, len(self.data_list)):
            temp=self.kmulti(self.data_list[i],self.weight_list[i])
            result = self.add(result, temp)

        # 还原数据集
        self.data_list = data_list
        return result

if __name__ == '__main__':
    data_list=[([0.31, 0.24], [0.73, 0.72]), ([0.97, 0.12], [0.12, 0.05]),
    ([0.8, 0.52], [0.73, 0.15]), ([0.91, 0.49], [0.42, 0.47]),([0.95, 0.06], [0.19, 0.1])]
    weight_list=[0.1, 0.2, 0.3, 0.1, 0.3],
    Pa = ReviceA(data_list)
    # Pa = GA(list1)
    # Pa = WA(list1,weight_list)
    # Pa = WGA(list1,weight_list)
    # Pa = OWA(list1,weight_list)
    print(Pa.getResult())